'''
https://leetcode.cn/problems/serialize-and-deserialize-n-ary-tree
'''
from typing import List, Optional

# Definition for a Node.
class Node(object):
    def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
        if children is None:
            children = []
        self.val = val
        self.children = children

class Codec:
    def serialize(self, root: 'Node') -> List:
        if not root:return []
        data=[root.val]
        if root.children:
            for child in root.children:
                data.append(self.serialize(child))
        return data

    def deserialize(self, data: List) -> 'Node':
        if not data:return None
        root=Node(data[0])
        root.children=[]
        for i in range(1,len(data)):
            root.children.append(self.deserialize(data[i]))
        return root
        
        
